package com.goodving.guli.bt.profile.scale;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;

import com.goodving.guli.view.C4220a;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * Created by john on 2015/11/25.
 */
public class XMLeScanCenter { // C3893a singleton class
    private static final String TAG = "BleScanCenter";
    private static XMLeScanCenter mBleScanCenter = null;
    private static final String f19589c = "AF";
    private final boolean f19590d;
    private final BluetoothAdapter mBleAdapter; // f19591e
    private BluetoothAdapter.LeScanCallback mLeScanCallback; //f19592f
    private HandlerThread mHandlerThread;
    private Handler mHandler;
    private final int f19595i;
    private final int f19596j;
    private final int f19597k;
    private final int f19598l;
    private final int f19599m;
    private final int f19600n;
    private final int f19601o;
    private volatile boolean f19602p;
    private Context mContext; // f19603q
    private List<FilterBleDevInfo> mFilterBleDevInfoList; //f19604r;
    private int f19605s;
    private int f19606t;
    private ScanCallback mScanCallback; //f19607u

    static {
        mBleScanCenter = null;
    }

    class MyHandler extends Handler { //C3894b
        final /* synthetic */ XMLeScanCenter bleScanCenter;

        MyHandler(XMLeScanCenter XMLeScanCenter, Looper looper) {
            super(looper);
            this.bleScanCenter = XMLeScanCenter;
        }

        public void handleMessage(Message message) {
            switch (message.what) {
                case 0 /*0*/:
                    this.bleScanCenter.start();
                    break; // TBD
                case 1 /*1*/:
                    this.bleScanCenter.stop();
                    break; //TBD
                case 2 /*2*/:
                    this.bleScanCenter.m25358b((C3907e) message.obj);
                    break; //TBD
                case 3 /*3*/:
                    this.bleScanCenter.stop();
                    Message message2 = new Message();
                    message2.what = 0;
                    message2.obj = message.obj;
                    sendMessageDelayed(message2, 0);
                    break; //TBD
                case 4 /*4*/:
                    FilterBleDevInfo filterBleDevInfo = (FilterBleDevInfo) message.obj;
                    if (this.bleScanCenter.mFilterBleDevInfoList.remove(filterBleDevInfo)) {
                        // start to connect the device.
                        filterBleDevInfo.m25379d().m13595b(filterBleDevInfo);
                    }
                    // if there is no filter device in the list, then stop the scan, and set the empty flag to true.
                    if (this.bleScanCenter.mFilterBleDevInfoList.size() < 1) {
                        this.bleScanCenter.f19602p = true;
                        sendEmptyMessage(1);
                    }
                    break; //TBD
                default:
                    break; //TBD
            }
        }
    }

    private XMLeScanCenter() {
        this.f19590d = false;
        this.mBleAdapter = BluetoothAdapter.getDefaultAdapter();
        this.mLeScanCallback = null;
        this.mHandlerThread = null;
        this.mHandler = null;
        this.f19595i = 0;
        this.f19596j = 3000;
        this.f19597k = 0;
        this.f19598l = 1;
        this.f19599m = 2;
        this.f19600n = 3;
        this.f19601o = 4;
        this.f19602p = false;
        this.mContext = null;
        this.mFilterBleDevInfoList = Collections.synchronizedList(new ArrayList());
        this.f19605s = 0;
        this.f19606t = 0;
        this.mScanCallback = null;
        this.mHandlerThread = new HandlerThread(TAG);
        this.mHandlerThread.start();
        this.mHandler = new MyHandler(this, this.mHandlerThread.getLooper());
    }

    public static XMLeScanCenter getInstance() { //m25345a
        if (mBleScanCenter == null) {
            mBleScanCenter = new XMLeScanCenter();
        }
        return mBleScanCenter;
    }

    private List<BluetoothDevice> m25346a(Context context) {
        HashMap hashMap = new HashMap();
        for (BluetoothDevice bluetoothDevice : ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getConnectedDevices(BluetoothProfile.GATT)) {
            hashMap.put(bluetoothDevice.getAddress(), bluetoothDevice);
        }
        Set<BluetoothDevice> bondedDevices = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
        if (bondedDevices != null && bondedDevices.size() > 0) {
            for (BluetoothDevice bluetoothDevice2 : bondedDevices) {
                if (!(bluetoothDevice2.getType() == 1 || hashMap.containsKey(bluetoothDevice2.getAddress()))) {
                    hashMap.put(bluetoothDevice2.getAddress(), bluetoothDevice2);
                }
            }
        }
        return new ArrayList(hashMap.values());
    }

    private void m25347a(BluetoothDevice bluetoothDevice, int i, byte[] bArr) {
        GuliLog.m12370d(TAG, "device:" + bluetoothDevice.getAddress() + ",rssi:" + i);
        // get the information from scanned device.
        // C3907e e = GattUtils.m25343e(bArr);//TBD
        C3907e e = FrkGattUtils.m25343e(bArr);//TBD
        e.btDev = bluetoothDevice;
        e.rssi = i;
        m25354a(e);
    }

    private void m25348a(BluetoothDevice bluetoothDevice, String str) {
        GuliLog.m12370d(TAG, "device:" + bluetoothDevice.getAddress() + ",uuid:" + str);
        C3907e c3907e = new C3907e();
        if (str != null) {
            c3907e.serv128.add(str);
        }
        c3907e.btDev = bluetoothDevice;
        c3907e.rssi = 0;
        m25354a(c3907e);
    }

    class C3896c implements C3895j {
        final /* synthetic */ UUID uuid;
        final /* synthetic */ XMLeScanCenter f19610b;

        C3896c(XMLeScanCenter XMLeScanCenter, UUID uuid) {
            this.f19610b = XMLeScanCenter;
            this.uuid = uuid;
        }

        public void m25374a(BluetoothDevice bluetoothDevice, ArrayList<UUID> arrayList) {
            Iterator it = arrayList.iterator();
            while (it.hasNext()) {
                UUID uuid = (UUID) it.next();
                GuliLog.m12370d("BleScanCenter", "device:" + bluetoothDevice.getAddress() + ",UUID:" + uuid.toString());
                if (this.uuid.equals(uuid) && (bluetoothDevice.getName() == null || !bluetoothDevice.getName().equals("AF"))) {
                    if (!this.f19610b.f19602p) {
                        this.f19610b.m25348a(bluetoothDevice, uuid.toString());
                    }
                    this.f19610b.f19606t = this.f19610b.f19606t + 1;
                    this.f19610b.f19605s = this.f19610b.f19605s - 1;
                    if (this.f19610b.f19605s == 0 && this.f19610b.f19606t == 0 && !this.f19610b.f19602p) {
                        this.f19610b.mHandler.sendEmptyMessage(0);
                        return;
                    }
                    return;
                }
            }
            this.f19610b.f19605s = this.f19610b.f19605s - 1;
            if (this.f19610b.f19605s == 0) {
            }
        }
    }

    private void m25349a(Context context, List<BluetoothDevice> list, UUID uuid) {
        GuliLog.m12370d(TAG, "filter UUID:" + uuid);
        this.f19605s = list.size();
        this.f19606t = 0;
        for (BluetoothDevice bluetoothDevice : list) {
            GuliLog.m12370d(TAG, "connected device:" + bluetoothDevice.getAddress());
            if (uuid == null) {
                m25348a(bluetoothDevice, null);
            } else {
                XMUUIDScanner.m25392a(context, bluetoothDevice, new C3896c(this, uuid));
            }
        }
        if (uuid == null) {
            this.mHandler.sendEmptyMessage(0);
        }
    }

    private void m25354a(C3907e c3907e) {
        Message message = new Message();
        message.what = 2;
        message.obj = c3907e;
        this.mHandler.sendMessage(message);
    }

    private void m25356b() {
        m25364e();
    }

    private void m25358b(C3907e c3907e) {
        if (c3907e != null) {
            BluetoothDevice bluetoothDevice = c3907e.btDev;
            if (bluetoothDevice != null) {
                String name = bluetoothDevice.getName();
                if (name != null && name.equals(f19589c)) {
                    GuliLog.m12370d(TAG, "filter AF~");
                    return;
                }
            }
            for (FilterBleDevInfo filterBleDevInfo : this.mFilterBleDevInfoList) {
                if (filterBleDevInfo.m25377b() != null) {
                    if (c3907e.m25434a(filterBleDevInfo.m25377b())) {
                        filterBleDevInfo.m25379d().onScanedDevice(c3907e, filterBleDevInfo);
                    }
                } else if (filterBleDevInfo.m25378c() == null) {
                    filterBleDevInfo.m25379d().onScanedDevice(c3907e, filterBleDevInfo);
                } else if (c3907e.btDev.getAddress().contains(filterBleDevInfo.m25378c())) {
                    filterBleDevInfo.m25379d().onScanedDevice(c3907e, filterBleDevInfo);
                }
            }
        }
    }

    class XMBleScanCallBack implements BluetoothAdapter.LeScanCallback { // C3897d
        final /* synthetic */ XMLeScanCenter bleScanCenter;

        XMBleScanCallBack(XMLeScanCenter XMLeScanCenter) {
            this.bleScanCenter = XMLeScanCenter;
        }

        public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bArr) {
            this.bleScanCenter.m25347a(bluetoothDevice, i, bArr);
        }
    }

    private void m25360c() {
        if (this.mLeScanCallback == null) {
            this.mLeScanCallback = new XMBleScanCallBack(this);
        }
        this.mBleAdapter.startLeScan(this.mLeScanCallback);
    }

    private void m25361d() {
        if (this.mLeScanCallback != null) {
            GuliLog.tracePrintInfo(TAG, "stopScan");
            try {
                this.mBleAdapter.stopLeScan(this.mLeScanCallback);
            } catch (Exception e) {
                GuliLog.m12370d(TAG, "stopLeScan exception:" + e.getMessage());
            } finally {
                this.mLeScanCallback = null;
            }
        }
    }

    private void m25364e() {
    }

    class C3898e extends ScanCallback {
        final /* synthetic */ XMLeScanCenter bleScanCenter;

        C3898e(XMLeScanCenter XMLeScanCenter) {
            this.bleScanCenter = XMLeScanCenter;
        }

        public void onBatchScanResults(List<ScanResult> list) {
            super.onBatchScanResults(list);
            GuliLog.tracePrintInfo("BleScanCenter", "onBatchScanResults:" + list.size());
        }

        public void onScanFailed(int i) {
            super.onScanFailed(i);
            GuliLog.tracePrintInfo("BleScanCenter", "onScanFailed:" + i);
        }

        public void onScanResult(int i, ScanResult scanResult) {
            GuliLog.tracePrintInfo("BleScanCenter", "onScanResult callbackType:" + i + ",result:" + scanResult);
            this.bleScanCenter.m25347a(scanResult.getDevice(), scanResult.getRssi(), scanResult.getScanRecord().getBytes());
        }
    }

    private void m25366f() {
        GuliLog.tracePrintInfo(TAG, "startForL");
        if (this.mScanCallback == null) {
            this.mScanCallback = new C3898e(this);
        }
        ScanSettings build = new ScanSettings.Builder().setReportDelay(0).setScanMode(2).build();
        this.mBleAdapter.getBluetoothLeScanner().startScan(new ArrayList(), build, this.mScanCallback);
    }

    private void m25368g() {
        GuliLog.tracePrintInfo(TAG, "stopForL");
        if (this.mScanCallback != null) {
            this.mBleAdapter.getBluetoothLeScanner().stopScan(this.mScanCallback);
            this.mScanCallback = null;
        }
    }

    private void m25370h() {
        GuliLog.tracePrintInfo(TAG, "flushForL");
        if (this.mScanCallback != null) {
            this.mBleAdapter.getBluetoothLeScanner().flushPendingScanResults(this.mScanCallback);
        }
    }

    private void start() {
        GuliLog.tracePrintInfo(TAG, "startScan");
        if (this.f19602p) {
            GuliLog.tracePrintInfo(TAG, "need stop,return!!!");
            return;
        }
        stop();
        m25360c();
        this.mHandler.sendEmptyMessageDelayed(3, 3000);
    }

    private void stop() {
        m25361d();
    }

    public void startScan(Context context, FilterBleDevInfo filterBleDevInfo) { // m25372a
        GuliLog.m12370d(TAG, "startScan<" + filterBleDevInfo + ">");
        if (filterBleDevInfo == null || this.mFilterBleDevInfoList.contains(filterBleDevInfo)) {
            GuliLog.m12370d(TAG, "null or exist BleScanOption,return now!!!");
            return;
        }
        this.f19602p = false;
        this.mFilterBleDevInfoList.add(filterBleDevInfo);
        if (!filterBleDevInfo.m25380e()) {
            // start to scan
            this.mHandler.sendEmptyMessage(0);
        } else if (context == null) {
            GuliLog.m12370d(TAG, "null Context,return now!!!");
            return;
        } else {
            // get all device which are connected or bonded.
            List a = m25346a(context.getApplicationContext());
            if (a == null || a.size() == 0) {
                // start to scan
                this.mHandler.sendEmptyMessage(0);
            } else {
                m25349a(this.mContext, a, filterBleDevInfo.m25377b());
            }
        }
        // to see if the timeout is bigger than 0, if yes, send message with delay.
        if (filterBleDevInfo.m25376a() > 0) {
            Message message = new Message();
            message.what = 4;
            message.obj = filterBleDevInfo;
            this.mHandler.sendMessageDelayed(message, (long) filterBleDevInfo.m25376a());
        }
    }

    public void m25373a(FilterBleDevInfo filterBleDevInfo) {
        GuliLog.m12370d(TAG, "stopScan<" + filterBleDevInfo + ">");
        if (filterBleDevInfo == null) {
            GuliLog.m12370d(TAG, "null BleScanOption,return now!!!");
            return;
        }
        Message message = new Message();
        message.what = 4;
        message.obj = filterBleDevInfo;
        this.mHandler.sendMessage(message);
    }
}
